package fr.flogo.destinee;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import fr.flogo.destinee.DestineeActivity.TextureDestineeSceneTest;
import fr.flogo.destinee.objects.IGLObject;
import fr.flogo.destinee.objects.statics.Cube;
import fr.flogo.destinee.objects.textures.TexturesUtils;

import android.content.Context;

public class World {
	private int mapWidth = 0;
	private List<IGLObject> mapObjects;
	
	private float[] startPosition = new float[] {0.0f,0.0f,0.0f};
	
	/**
	 * @return the startPosition
	 */
	public float[] getStartPosition() {
		return startPosition;
	}

	public World(Context context, String fileName, TexturesUtils texturesUtils) throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(context.getAssets().open(fileName)));
		String line = null;
		int i;
		int nLine = 0;
		this.mapObjects = new ArrayList<IGLObject>();
		mapWidth = 0;
		while((line = reader.readLine()) != null) {
			if (mapWidth == 0) {
				mapWidth = line.length();
			} else if (mapWidth != line.length()) {
				throw new Exception("Error in world file :" + fileName + " line "+nLine+" -> Not same number of line");
			}
			if (mapWidth == 0) {
				throw new Exception("Error in world file :" + fileName + " line "+nLine+" -> No empty line allowed");
			}
			i = 0;
			Cube cube = null;
			float y;
			while (i < mapWidth) {
				y = 0.0f;
				// TODO Faire un pointeur sur fonction + factory. Ajouter static public IGlObject instanciate dans le IGlObject;
				if (line.charAt(i) == 'X') {
					cube = new Cube(texturesUtils.getTexture(
							TextureDestineeSceneTest.TexturesNames.Mur.ordinal())
							.get(0));
					System.out.print("New Wall ");
					
				} else if (line.charAt(i) == ' ') {
					cube = new Cube(texturesUtils.getTexture(
							TextureDestineeSceneTest.TexturesNames.Sol.ordinal())
							.get(0));
					y = -2f;
					System.out.print("New Floor ");
				} else if (line.charAt(i) == 'D') {
					cube = new Cube(texturesUtils.getTexture(
							TextureDestineeSceneTest.TexturesNames.Sol.ordinal())
							.get(0));
					System.out.print("New Start ");
					this.startPosition[0] = i;
					this.startPosition[1] = 0f;
					this.startPosition[2] = nLine;
				} else if (line.charAt(i) == 'P') {
					cube = new Cube(texturesUtils.getTexture(
							TextureDestineeSceneTest.TexturesNames.Voute.ordinal())
							.get(0));
					System.out.print("New Door ");
				} else {
					throw new Exception("Error in world file :" + fileName + " line "+nLine+" -> unkown char ["+line.charAt(i)+"]");
				}
				System.out.println("Position : ["+i+"f, 0f, "+nLine+"f]");
				cube.translate(i*2, y, nLine*2);
				this.mapObjects.add(cube);
				i++;
			}
			nLine++;
		}
	}
	
	public void draw(GL10 gl) {
		// ne garder qu'un tableau en static
		Iterator<IGLObject> it = this.mapObjects.iterator();
		while (it.hasNext()) {
			IGLObject object = it.next();
			object.draw(gl);
		}
	}
}
